import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    // 只有router跳转到 path ('/') 时,此方法才会被调用
    beforeEnter: (to, from, next) => {
      console.log('导航独享守卫:beforeEnter')
      next()
    }
  },
  {
    // 动态路由匹配，原来的组件实例会被复用，组件的生命周期钩子不会再被调用
    // https://router.vuejs.org/zh/guide/essentials/dynamic-matching.html
    path: '/about/:id',
    name: 'About',
    component: () => import('../views/About.vue')
  }
]

const router = new VueRouter({
  routes
})

// 全局导航守卫，所有router跳转均会调用，调用时机看log
// afterEach 无next

router.beforeEach((to, from, next) => {
  console.log('导航前置守卫:beforeEach')
  next()
})

router.afterEach((to, from) => {
  console.log('导航后置守卫:afterEach')
})

router.beforeResolve((to, from, next) => {
  console.log('导航解析守卫:beforeResolve')
  next()
})

export default router
